package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.exception.BusinessException;
import com.demo.pojo.dto.StudentListDTO;
import com.demo.pojo.entity.Student;
import com.demo.service.StudentService;
import com.demo.mapper.StudentMapper;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【student】的数据库操作Service实现
* @createDate 2025-09-11 17:15:14
*/
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    private final StudentMapper studentMapper;

    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }


    @Override
    public Student selectById(Integer id) {
        //根据学生id查询学生信息
        Student student = studentMapper.selectById(id);
        //如果没查到抛出异常
        if (student == null){
            throw new BusinessException("没有此学生信息");
        }
        log.info("查询学生信息成功:{}", student);
        return student;
    }

    @Override
    public void saveStudent(Student student) {
        //新增前判断学号有无重复
        if (studentMapper.selectByNO(student.getStNo()) != null){
            throw new BusinessException("学号重复");
        }
        //执行新增操作
        studentMapper.insert(student);
        log.info("新增学生信息成功:{}", student);
    }


    @Override
    public void updateStudent(Student student) {
        //修改前判断是否存在该学生
        Student oldStudent = studentMapper.selectById(student.getStId());
        if (oldStudent == null){
            throw new BusinessException("没有此学生信息");
        }
        //修改前判断学号有无变动
        //如果查询出的学号和修改前的学号一致，则不进行修改
        if ( !oldStudent.getStNo().equals(student.getStNo())){
           //学号不一致，说明学号被修改，查询有无重复
            if (studentMapper.selectByNO(student.getStNo()) != null){
                throw new BusinessException("学号重复");
            }
        }

    }

    @Override
    public Map<String, Object> listPage(StudentListDTO dto) {
        //1. 创建分页对象
        Page<Student> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        // 2. 构建查询条件
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<Student>()
                .eq(dto.getStNo() != null && dto.getStNo() != -1, Student::getStNo, dto.getStNo())
                .like(StringUtils.isNotBlank(dto.getStName()), Student::getStName, dto.getStName())
                .eq(dto.getStSex() != null && dto.getStSex() != -1, Student::getStSex, dto.getStSex())
                .like(StringUtils.isNotBlank(dto.getStPhone()), Student::getStPhone, dto.getStPhone())
                .like(StringUtils.isNotBlank(dto.getStEmail()), Student::getStEmail, dto.getStEmail())
                .like(StringUtils.isNotBlank(dto.getStQq()), Student::getStQq, dto.getStQq())
                .eq(dto.getStClassId() != null && dto.getStClassId() != -1, Student::getStClassId, dto.getStClassId())
                ;

        // 3. 执行分页查询
        Page<Student> pageResult = studentMapper.selectPage(page, lambdaQueryWrapper);

        log.info("分页查询结果：{}", pageResult);
        // 4. 返回结果
        return Map.of(
                "records", pageResult.getRecords(),
                "total", pageResult.getTotal(),
                "current", pageResult.getCurrent()
        );
    }

    @Override
    public List<StudentListDTO> getStudentList() {
        // 查询所有学生
        List<Student> students = list();
        List<StudentListDTO> studentListDTOs = new ArrayList<>();
        
        for (Student student : students) {
            StudentListDTO dto = new StudentListDTO();
            dto.setStId(student.getStId());
            dto.setStNo(student.getStNo());
            dto.setStName(student.getStName());
            dto.setStSex(student.getStSex());
            dto.setStPhone(student.getStPhone());
            dto.setStEmail(student.getStEmail());
            dto.setStQq(student.getStQq());
            dto.setStRemark(student.getStRemark());
            dto.setStClassId(student.getStClassId());
            studentListDTOs.add(dto);
        }
        
        return studentListDTOs;
    }


}


